package org.sabayframework.cluster;

import org.sabayframework.activity.Activity;
import org.sabayframework.activity.ActivityContext;
import org.sabayframework.activity.impl.AbstractActivity;

/**
 * Class provides information required for distributed execution of an activity. 
 * */
public class DistributedActivity<I,O> extends AbstractActivity<I, O>{

	/**
	 * Activity to execute.
	 * */
	private Activity<I,O> activity;
	/**
	 * Distribution strategy for the activity.
	 * */
	private DistributionStrategy distributionStrategy;
	/**
	 * List of packages that must be imported in order to execute activity.
	 * See documentation on Import-Package header of OSGi bundle. 
	 * */
	private String importPackage;
	/**
	 * List of symbolic names of OSGi bundles that are required to execute activity. 
	 * See documentation on Require-Bundle header of OSGi bundle.
	 * */
	private String requireBundle;
	
	
	public DistributedActivity(Activity<I, O> activity,
			DistributionStrategy distributionStrategy) {
		this(activity, distributionStrategy, null);
	}
	public DistributedActivity(Activity<I, O> activity,
			DistributionStrategy distributionStrategy, String importPackage) {
		this(activity, distributionStrategy, importPackage, null);
	}
	public DistributedActivity(Activity<I, O> activity,
			DistributionStrategy distributionStrategy, String importPackage,
			String requireBundle) {
		super();
		this.activity = activity;
		this.distributionStrategy = distributionStrategy;
		this.importPackage = importPackage;
		this.requireBundle = requireBundle;
	}
	
	public Activity<I, O> getActivity() {
		return activity;
	}
	public DistributionStrategy getDistributionStrategy() {
		return distributionStrategy;
	}
	public String getRequireBundle() {
		return requireBundle;
	}
	public String getImportPackage() {
		return importPackage;
	}
	
	public void setActivity(Activity<I, O> activity) {
		this.activity = activity;
	}
	public void setDistributionStrategy(DistributionStrategy distributionStrategy) {
		this.distributionStrategy = distributionStrategy;
	}
	public void setImportPackage(String importPackage) {
		this.importPackage = importPackage;
	}
	public void setRequireBundle(String requireBundle) {
		this.requireBundle = requireBundle;
	}
	@Override
	public void execute(ActivityContext<I, O> ctx) throws Exception {
		Cluster cluster = ClusterContext.getClusteringService().getCluster();
		cluster.getExecutionQueue().submit(this, ctx.getInput(), ctx.getOutput());
	}
	
	
}
